﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using MySql.Data.MySqlClient;
using FetchDataSystem.Model;

namespace FetchDataSystem.DAL
{
    public class FetchDataService
    {
        public static int InsertDecaptitating(WeightRecords records)
        {
            int result = 0;
            string sql = @"INSERT into log_DecaptitatingBizRecords VALUES(
                            @id,@serialNumber,@cardId,@employeeId,@employeeName,@workGroupId,@workGroupName,
                            @workCellId,@workCellName,@combineId,@combineName,@weight,@unit,@time,
                            @creatorId,@creatorName,@createDate,@modifer,@modifiedDate,@remarks
                            );";
            MySqlParameter[] pars = new MySqlParameter[] {
                new MySqlParameter("@id",records.Id),
                new MySqlParameter("@serialNumber",records.QrCode),
                new MySqlParameter("@cardId",records.CardId),
                new MySqlParameter("@employeeId",records.EmployeeId),
                new MySqlParameter("@employeeName",records.EmployeeName),
                new MySqlParameter("@workGroupId",records.WorkCellId),
                new MySqlParameter("@workGroupName",records.WorkCellName),
                new MySqlParameter("@workCellId",records.WorkCellId),
                new MySqlParameter("@workCellName",records.WorkCellName),
                new MySqlParameter("@combineId",records.EquipId),
                new MySqlParameter("@combineName",records.EquipName),
                new MySqlParameter("@weight",records.Weight),
                new MySqlParameter("@unit",records.Unit),
                new MySqlParameter("@time",records.Time),
                new MySqlParameter("@creatorId",records.CreatorId),
                new MySqlParameter("@createDate",records.CreateDate),
                new MySqlParameter("@modifer",records.UpdateBy),
                new MySqlParameter("@modifiedDate",records.UpdateDate),
                new MySqlParameter("@remarks",records.Remarks)
            };
            try
            {
                result = DBHelper.ExecuteNonQuery(DBHelper.ConnectionString, CommandType.Text, sql, pars);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return 0;
            }
            return result;
        }
        public static int InsertRemoveBoneRecords(WeightRecords records)
        {
            int result = 0;
            string sql = @"INSERT into log_RemoveFishBoneBizRecords VALUES(
                            @id,@serialNumber,@cardId,@employeeId,@employeeName,@workGroupId,@workGroupName,
                            @workCellId,@workCellName,@combineId,@combineName,@weight,@unit,@time,
                            @creatorId,@creatorName,@createDate,@modifer,@modifiedDate,@remarks
                            );";
            MySqlParameter[] pars = new MySqlParameter[] {
                new MySqlParameter("@id",records.Id),
                new MySqlParameter("@serialNumber",records.QrCode),
                new MySqlParameter("@cardId",records.CardId),
                new MySqlParameter("@employeeId",records.EmployeeId),
                new MySqlParameter("@employeeName",records.EmployeeName),
                new MySqlParameter("@workGroupId",records.WorkCellId),
                new MySqlParameter("@workGroupName",records.WorkCellName),
                new MySqlParameter("@workCellId",records.WorkCellId),
                new MySqlParameter("@workCellName",records.WorkCellName),
                new MySqlParameter("@combineId",records.EquipId),
                new MySqlParameter("@combineName",records.EquipName),
                new MySqlParameter("@weight",records.Weight),
                new MySqlParameter("@unit",records.Unit),
                new MySqlParameter("@time",records.Time),
                new MySqlParameter("@creatorId",records.CreatorId),
                new MySqlParameter("@creatorName",Session.LoginName),
                new MySqlParameter("@createDate",records.CreateDate),
                new MySqlParameter("@modifer",records.UpdateBy),
                new MySqlParameter("@modifiedDate",records.UpdateDate),
                new MySqlParameter("@remarks",records.Remarks)
            };
            try
            {
                result = DBHelper.ExecuteNonQuery(DBHelper.ConnectionString, CommandType.Text, sql, pars);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return 0;
            }
            return result;
        }
        public static int GetCountByEmployeeIdAndTime(string employeeId,DateTime startTime,string flag)
        {
            string stationName = null;
            switch (flag)
            {
                case "cut":
                    stationName = "log_CutPieceBizRecords";
                    break;
                case "piece":
                    stationName = "log_ChopingBizRecords";
                    break;
            }
            int result = 0;
            string sql = string.Format("SELECT sum(counter) from {0} WHERE employeeId=@employeeId AND time>=@startTime",stationName);
            try
            {
                object obj = DBHelper.ExecuteScalar(DBHelper.ConnectionString, CommandType.Text, sql, new MySqlParameter[]
                {
                    new MySqlParameter("@employeeId",employeeId),
                    new MySqlParameter("@startTime",startTime)
                });
                if (obj != null)
                {
                    result = Convert.ToInt32(obj);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return 0;
            }

            return result;
        }

        public static int InsertCutPieceRecords(CutPieceRecords records,string flag)
        {
            string stationName = null;
            switch (flag)
            {
                case "cut":
                    stationName = "log_CutPieceBizRecords";
                    break;
                case "piece":
                    stationName = "log_ChopingBizRecords";
                    break;
            }
            int result = 0;
            string sql = string.Format(@"INSERT into {0} VALUES(
                            @id,@serialNumber,@cardId,@employeeId,@employeeName,@workGroupId,@workGroupName,
                            @workCellId,@workCellName,@combineId,@combineName,@counter,@pieces,@unit,@time,
                            @creatorId,@creatorName,@createDate,@modifer,@modifiedDate,@remarks);",stationName);
            MySqlParameter[] pars = new MySqlParameter[] {
                new MySqlParameter("@id",records.Id),
                new MySqlParameter("@serialNumber",records.QrCode),
                new MySqlParameter("@cardId",records.CardId),
                new MySqlParameter("@employeeId",records.EmployeeId),
                new MySqlParameter("@employeeName",records.EmployeeName),
                new MySqlParameter("@workGroupId",records.WorkCellId),
                new MySqlParameter("@workGroupName",records.WorkCellName),
                new MySqlParameter("@workCellId",records.WorkCellId),
                new MySqlParameter("@workCellName",records.WorkCellName),
                new MySqlParameter("@combineId",records.EquipId),
                new MySqlParameter("@combineName",records.EquipName),
                new MySqlParameter("@counter",records.Counter),
                new MySqlParameter("@pieces",records.Piececs),
                new MySqlParameter("@unit",records.Unit),
                new MySqlParameter("@time",records.Time),
                new MySqlParameter("@creatorId",records.CreatorId),
                new MySqlParameter("@creatorName",Session.LoginName),
                new MySqlParameter("@createDate",records.CreateDate),
                new MySqlParameter("@modifer",records.UpdateBy),
                new MySqlParameter("@modifiedDate",records.UpdateDate),
                new MySqlParameter("@remarks",records.Remarks)
            };
            try
            {
                result = DBHelper.ExecuteNonQuery(DBHelper.ConnectionString, CommandType.Text, sql, pars);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return 0;
            }
            return result;
        }
        public static int InsertCutPieceQualityTestingRecords(CutPieceQualityTestingRecords records)
        {
            int result = 0;
            string sql = @"INSERT into log_cutpiecequalitytestingrecords VALUES(
                            @id,@cardId,@employeeId,@employeeName,@workCellId,@workCellName,
                            @equipId,@equipName,@qrCode,@weight,@unit,@error,@qualified,
                            @time,@del_flag,@remarks,@createBy,@createDate,@updateBy,@updateDate
                            );";
            MySqlParameter[] pars = new MySqlParameter[] {
                new MySqlParameter("@id",records.Id),
                new MySqlParameter("@cardId",records.CardId),
                new MySqlParameter("@employeeId",records.EmployeeId),
                new MySqlParameter("@employeeName",records.EmployeeName),
                new MySqlParameter("@workCellId",records.WorkCellId),
                new MySqlParameter("@workCellName",records.WorkCellName),
                new MySqlParameter("@equipId",records.EquipId),
                new MySqlParameter("@equipName",records.EquipName),
                new MySqlParameter("@qrCode",records.QrCode),
                new MySqlParameter("@weight",records.Weight),
                new MySqlParameter("@unit",records.Unit),
                new MySqlParameter("@error",records.Error),
                new MySqlParameter("@qualified",records.Qualified),
                new MySqlParameter("@time",records.Time),
                new MySqlParameter("@del_flag",records.DelFlag),
                 new MySqlParameter("@remarks",records.Remarks),
                new MySqlParameter("@createBy",records.CreateBy),
                new MySqlParameter("@createDate",records.CreateDate),
                new MySqlParameter("@updateBy",records.UpdateBy),
                new MySqlParameter("@updateDate",records.UpdateDate)
            };
            try
            {
                result = DBHelper.ExecuteNonQuery(DBHelper.ConnectionString, CommandType.Text, sql, pars);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return 0;
            }
            return result;
        }
    }
}
